package com.dogkr.server.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.HostApply;
import com.dogkr.server.mapper.HostApplyMapper;
import com.dogkr.server.mapper.HostMapper;
import com.dogkr.server.mapper.OrderMapper;
import com.sean.common.page.Pagination;
import com.sean.common.page.ResultPagination;
import com.sean.common.service.BaseService;
import com.sean.common.utils.StringUtils;


@Service
public class HostService extends BaseService {
	
	@Autowired
	private HostMapper hostMapper;
	@Autowired
	private HostApplyMapper hostApplyMapper;
	
	@Autowired
	private OrderMapper orderMapper;
	
	public void update(Host host) {
		host.setLastModifydate(new Date());
		hostMapper.updateByPrimaryKeySelective(host);
	}
	
	public ResultPagination<Host> selectWithSearch(Pagination pagination, String word) {
		pagination.putParam("word", word);
		return this.selectListForPage(HostMapper.class, "selectWithSearch", pagination, Host.class);
	}

	public Host selectByIdWithAll(Long id) {
		return hostMapper.selectByIdWithAll(id);
	}
	
	public Host selectByUserKey(Long userId) {
		return hostMapper.selectByUserKey(userId);
	}

	public List<Host> selectByFavorites(Long userId) {
		return hostMapper.selectByFavorites(userId);
	}

	public Host selectByPrimaryKey(Long hostId) {
		return hostMapper.selectByPrimaryKey(hostId);
	}

	public Host selectWithShow(Long id) {
		return hostMapper.selectByIdWithShow(id);
	}

	public void saveApply(HostApply apply) {
		if(apply.getId() == null) {
			hostApplyMapper.insert(apply);
		} else {
			hostApplyMapper.updateByPrimaryKeySelective(apply);
		}
	}

	public HostApply getApplyByUserId(Long userId) {
		return hostApplyMapper.selectByUserKey(userId);
	}
	/**
	 * 更新评论数量
	 * @param hostId
	 */
	public void updateAppraise(Long hostId) {
		int count = orderMapper.countAppraiseByHostKey(hostId);
		Host record = new Host();
		record.setAssessNum(count);
		record.setId(hostId);
		hostMapper.updateByPrimaryKeySelective(record);
	}
}
